<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="tokenList" border>
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="商家名称" align="center" prop="name" />
      <el-table-column label="商户Id" align="center" prop="venderId" />
      <el-table-column label="商户名" align="center" prop="userNick" />
      <el-table-column label="token" align="center" prop="token" />
      <el-table-column label="有效期(精确到秒)" align="center" prop="expiresIn" />
      <el-table-column label="token创建时间(精确到毫秒)" align="center" prop="time" />
      <el-table-column label="京东到家key" align="center" prop="appKey" />
      <el-table-column label="京东到家秘钥" align="center" prop="appSecret" />
      <el-table-column label="商家门店编码" align="center" prop="outStationNo" />
      <el-table-column label="到家门店编码" align="center" prop="stationNo" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:token:edit']">修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>

    <!-- 添加或修改京东到家认证信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="180px" label-suffix="：">
        <el-form-item label="商家名称" prop="name">
          {{form.name}}
        </el-form-item>
        <el-form-item label="商户Id" prop="venderId">
          {{form.venderId}}
        </el-form-item>
        <el-form-item label="商户名" prop="userNick">
          {{form.userNick}}
        </el-form-item>
        <el-form-item label="token" prop="token">
          <el-input v-model="form.token" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="有效期(秒)" prop="expiresIn">
          {{form.expiresIn}}
        </el-form-item>
        <el-form-item label="token创建时间(毫秒)" prop="time">
          {{form.time}}
        </el-form-item>
        <el-form-item label="京东到家key" prop="appKey">
          {{form.appKey}}
        </el-form-item>
        <el-form-item label="京东到家秘钥" prop="appSecret">
          <el-input v-model="form.appSecret" placeholder="请输入京东到家秘钥" />
        </el-form-item>
        <el-form-item label="商家门店编码" prop="outStationNo">
          {{form.outStationNo}}
        </el-form-item>
        <el-form-item label="到家门店编码" prop="stationNo">
          {{form.stationNo}}
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listToken, getToken, addToken, updateToken } from "@/api/erp/jd_token";

export default {
  name: "Token",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 京东到家认证信息表格数据
      tokenList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bmbh: null,
        name: null,
        uid: null,
        venderId: null,
        userNick: null,
        token: null,
        expiresIn: null,
        time: null,
        appKey: null,
        appSecret: null,
        outStationNo: null,
        stationNo: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        bmbh: [
          { required: true, message: "部门编号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询京东到家认证信息列表 */
    getList() {
      this.loading = true;
      listToken(this.queryParams).then(response => {
        this.tokenList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        bmbh: null,
        name: null,
        uid: null,
        venderId: null,
        userNick: null,
        token: null,
        expiresIn: null,
        time: null,
        appKey: null,
        appSecret: null,
        outStationNo: null,
        stationNo: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getToken(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改京东到家认证信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateToken(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addToken(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    }
  }
};
</script>
